package com.nav.tictactoe;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class WaitActivity extends Activity {
	Button startBut, removeBut;
	Broadcaster broadcaster;
	ConnectionAcceptor acceptor;
	Thread broadcasterThread, acceptorThread;
	TextView opponent;
	
    @SuppressLint("NewApi")
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	// set view
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wait);
        
        // behavior
		startBut = (Button)findViewById(R.id.Start);
		startBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					SharedData.opponent.getOutputStream().write(CommunicationProtocol.START.getBytes());
				} catch(Exception e) {
					AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(v.getContext());
					dialogBuilder.setTitle("Error communicating start to the opponent");
					dialogBuilder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
						}
					});
					dialogBuilder.setMessage(e.getMessage());
					AlertDialog alertDialog = dialogBuilder.create();
					alertDialog.show();
				}
				Intent intent = new Intent(v.getContext(), GameActivity.class);
				startActivity(intent);
			}
		});
		broadcaster = new Broadcaster(this);
    	broadcasterThread = new Thread(broadcaster);
    	acceptor = new ConnectionAcceptor(this);
    	acceptorThread = new Thread(acceptor);
		opponent = (TextView)findViewById(R.id.Opponent);
		removeBut = (Button)findViewById(R.id.Remove);
		removeBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(SharedData.opponent != null) {
					try {
						SharedData.opponent.getOutputStream().write(CommunicationProtocol.REMOVED.getBytes());
					} catch(Exception e) {
						AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(v.getContext());
						dialogBuilder.setTitle("Error communicating removal to the opponent");
						dialogBuilder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
							}
						});
						dialogBuilder.setMessage(e.getMessage());
						AlertDialog alertDialog = dialogBuilder.create();
						alertDialog.show();
					}
					SharedData.opponent = null;
				}
	    		opponent.setText("");
	    		startBut.setEnabled(false);
	    		if(!broadcasterThread.isAlive())
	    			broadcasterThread.start();
	        	acceptorThread.start();
			}
		});
		removeBut.callOnClick();
		
        // initialize
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
